package cn.wolfcode.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.mapper.ActivityItemMapper;
import cn.wolfcode.mapper.ChoiceMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private ActivityItemMapper activityItemMapper;
 @Autowired
    private ChoiceMapper choiceMapper;

    @RequiredPermission(name="活动页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
        }
        return "activity/input";
    }
    @RequestMapping("/page")
    public String page(Long id, Model model, HttpSession session){
      List<ActivityItem> items= activityItemMapper.queryByActivityId(id);
      model.addAttribute("items",items);
      session.setAttribute("activityId",id);
      return "activity/page";
    }
    @RequestMapping("/result")
    public String result(Long id,Model model,HttpSession session){
        Long activityId = (Long)session.getAttribute("activityId");

        activityItemMapper.saveNum(id);
        Choice choice = new Choice();
        choice.setCreateTime(new Date());
        choice.setEmployeeId(UserContext.getCurrentUser().getId());
        choice.setActivityItemId(id);
        choiceMapper.insert(choice);

        List<ActivityItem> items= activityItemMapper.queryByActivityId(activityId);
        Integer countNum = activityItemMapper.selectCountNum(activityId);
        model.addAttribute("items",items);
        model.addAttribute("countNum",countNum);

        return "activity/result";
    }

    @RequestMapping("/read")
    public String read(Long id,Model model){

        List<ActivityItem> items= activityItemMapper.queryByActivityId(id);
        Integer countNum = activityItemMapper.selectCountNum(id);
        model.addAttribute("items",items);
        model.addAttribute("countNum",countNum);
        return "activity/result";
    }

    @RequestMapping("/vote")
    @ResponseBody
    public JsonResult vote(Long id){


        JsonResult result = new JsonResult();
        if ((choiceMapper.selectByEmployeeId(UserContext.getCurrentUser().getId(),id))==null){
            result.setSuccess(true);

        }else{
            result.setSuccess(false);
            result.setMsg("你已投过票");
        }
        return result;
    }
}
